package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.Classes;
import com.wanmait.schedule.entity.CoursePlan;
import com.wanmait.schedule.entity.Teacher;
import com.wanmait.schedule.mapper.ClassesMapper;
import com.wanmait.schedule.mapper.CoursePlanMapper;
import com.wanmait.schedule.mapper.TeacherMapper;
import com.wanmait.schedule.service.CoursePlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 课程计划表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@Service
public class CoursePlanServiceImpl extends ServiceImpl<CoursePlanMapper, CoursePlan> implements CoursePlanService {

    @Autowired
    private CoursePlanMapper coursePlanMapper;

    @Autowired
    private ClassesMapper classesMapper;


    /**
     * 带分页 计划列表 包含模糊查询
     * @param coursePlan
     * @param pageNum
     * @return
     */
    @Override
    public PageInfo<CoursePlan> findAll(CoursePlan coursePlan, Integer pageNum) {
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<CoursePlan> conditions = coursePlanMapper.findConditions();
        PageInfo<CoursePlan> pageInfo = new PageInfo<>(conditions, 11);
        return pageInfo;
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Classes findById(Integer id) {
        Classes classes = classesMapper.selectById(id);
        classes.setCoursePlanList(coursePlanMapper.findCoursePlan(id));
        return classes;
    }

    /**
     * 根据课程id  查找教师
     * @param id
     * @return
     */
    @Override
    public List<Teacher> findCourseIdByTeacher(Integer id) {
        List<Teacher> courseIdByTeacher = coursePlanMapper.findCourseIdByTeacher(id);
        return courseIdByTeacher;
    }

    @Override
    public Boolean save(Map map) {

        System.out.println("================================");
        //新计划
        List<CoursePlan> addCoursePlans = new ArrayList<>();
        //更新计划
        List<CoursePlan> updataCoursePlans = new ArrayList<>();
        //删除计划
        List<Integer> delCoursePlans = new ArrayList<>();

        //新计划的集合
        List<Map> newCourseList = (List<Map>) map.get("newCourseList");

        for (Map o : newCourseList) {
            CoursePlan coursePlan = new CoursePlan();

            try {
                coursePlan.setClassId((Integer) o.get("classId"));
                coursePlan.setCourseId((Integer) o.get("courseId"));
                coursePlan.setTeacherId((Integer) o.get("teacherId"));
                coursePlan.setWeekCount((Integer) o.get("weekCount"));
                coursePlan.setWeight((Integer) o.get("weight"));
                coursePlan.setInputId(1);
                addCoursePlans.add(coursePlan);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        //更新计划的集合
        List<Map> updataCoursePlanList = (List<Map>) map.get("updataCourseList");

        for (Map o : updataCoursePlanList) {

            CoursePlan coursePlan = new CoursePlan();
            try {
                coursePlan.setInputId((Integer) o.get("inputId"));
                coursePlan.setId((Integer) o.get("id"));
                coursePlan.setClassId((Integer) o.get("classId"));
                coursePlan.setCourseId((Integer) o.get("courseId"));
                coursePlan.setTeacherId((Integer) o.get("teacherId"));
                coursePlan.setWeekCount((Integer) o.get("weekCount"));
                coursePlan.setWeight((Integer) o.get("weight"));
                updataCoursePlans.add(coursePlan);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        //删除计划的集合
        List<Integer> delCourseList = (List<Integer>) map.get("dleCourseList");

        if(delCourseList.size()>0){
            delCoursePlans = delCourseList;
        }

        if(addCoursePlans.size()>0){
            coursePlanMapper.batchInsert(addCoursePlans);
        }

        if(updataCoursePlans.size()>0){
            for (CoursePlan updataCoursePlan : updataCoursePlans) {
                coursePlanMapper.updateById(updataCoursePlan);
            }
        }

        if(delCoursePlans.size()>0){
            coursePlanMapper.batchUpdate(delCoursePlans);
        }


//        System.out.println(newCourseList.get(0));

        return true;
    }
}
